package com.mall.server.controller;

import cn.hutool.core.collection.CollUtil;
import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.UmsRoleService;
import com.tanhua.model.domain.UmsMenu;
import com.tanhua.model.domain.UmsRole;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping("/listAll")
    public R roleListAll() {
        List<UmsRole> umsRole = umsRoleService.roleListAll();
        if (CollUtil.isEmpty(umsRole)) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(umsRole);
    }

    /**
     * 根据角色名称分页获取角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public R roleList(String keyword, Integer pageNum, Integer pageSize) {

        PageVo vo = umsRoleService.roleList(keyword, pageNum, pageSize);

        if (vo == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(vo);
    }

    /**
     * 获取角色相关菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listMenu/{roleId}")
    public R listMenu(@PathVariable Integer roleId) {
        List<UmsMenu> menu = umsRoleService.listMenu(roleId);
        return R.success(menu);
    }


    /**
     * 给角色分配菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/allocMenu")
    public R allocMenu(Integer roleId, Integer[] menuIds) {
        umsRoleService.allocMenu(roleId, menuIds);
        return R.success("操作成功");
    }

    /**
     * 添加角色
     *
     * @param umsRole
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsRole umsRole) {
        umsRoleService.create(umsRole);
        return R.success("操作成功");
    }


    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R deletes(Integer[] ids) {
        umsRoleService.deletes(ids);
        return R.success("操作成功");
    }

    /**
     * 获取角色相关资源
     *
     * @param roleId
     * @return
     */
    @GetMapping("/listResource/{roleId}")
    public R listResources(@PathVariable Integer roleId) {
        UmsRole menu = umsRoleService.listResources(roleId);
        return R.success(menu);
    }


    /**
     * 给角色分配资源
     */
    @PostMapping("/allocResource")
    public R allocResource(Integer roleId, Integer[] resourceIds) {
        umsRoleService.allocResource(roleId, resourceIds);
        return R.success("操作成功");
    }


    /**
     * 修改角色状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(Integer status,@PathVariable Integer id) {
        umsRoleService.updateStatus(status,id);
        return R.success("操作成功");
    }


}
